<?php
    $news_id = $_POST['news_id'];
    $news_title = addslashes($_POST['news_title']);
    $news_location = addslashes($_POST['news_location']);
    $news_date = strtotime($_POST['news_date']);
    $news_msg_1 = addslashes($_POST['news_msg_1']);
    $news_msg_2 = addslashes($_POST['news_msg_2']);
    $news_video = addslashes($_POST['news_video']);
    $news_video_post = addslashes($_POST['news_video_post']);
    
    $sql = "UPDATE news SET
    news_title='$news_title',
    news_date='$news_date',
    news_location='$news_location',
    news_msg_1='$news_msg_1',
    news_msg_2='$news_msg_2',
    news_video='$news_video',
    news_video_pos='$news_video_post' WHERE news_id=$news_id";
    $result = $db->Execute($sql);
    
    if($result){
        $msg = 'บันทึกข้อมูลเรียบร้อยแล้ว';
        if($_FILES['news_img']['name']!='')
        {
            $handle = new Upload($_FILES['news_img']);
            if ($handle->uploaded)
            {
                $handle->image_resize = true;
                $handle->image_ratio_fill = true;
                $handle->image_background_color = '#ffffff';
                $handle->image_x = 254;
                $handle->image_y = 134;
                
                $handle->Process(APPPATH.'/images/news');
    
                if ($handle->processed)
                {
                    $sql = 'UPDATE news SET news_img="'.$handle->file_dst_name.'" WHERE news_id='.$news_id;
                    $result =$db->Execute($sql);
                    if($result)
                    {
                        $msg = 'บันทึกข้อมูลเรียบร้อยแล้ว';
                        if(img_exists(APPPATH.'/images/news',$_POST['img_old'])){
                            @unlink(APPPATH.'/images/news/'.$_POST['img_old']);
                        }
                    }
                    else
                    {
                        $msg = 'ไม่สามารถอัพโหลดรูปได้';
                        if(img_exists(APPPATH.'/images/news',$handle->file_dst_name)){
                            @unlink(APPPATH.'/images/news/'.$handle->file_dst_name);
                        }
                    }
                }
                else
                {
                    $msg = 'ไม่สามารถอัพโหลดรูปได้';
                }
    
            }
            else
            {
                $msg = 'ไม่สามารถอัพโหลดรูปได้';
            }
        }
    }else{
        $msg = 'ไม่สามารถบันทึกข้อมูลได้';
    }
?>

<?php
    include('header.php');
?>
<div class="grid_24" id="contents">
    <div id="query_result">
    <?php
        echo $msg , ' <img src="images/ajax-loader.gif" />';
        redirect('?m=news&s=news-edit&news_id='.$news_id,3);
    ?>
    </div>
</div><!-- end .grid_18 -->
<div class="clear">&nbsp;</div>
<?php
    include('footer.php');
?>